package org.charles.zhifou.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.charles.zhifou.common.annotation.AuthCheck;
import org.charles.zhifou.common.response.BaseResponse;
import org.charles.zhifou.common.response.ResponseUtils;
import org.charles.zhifou.enums.UserRoleEnum;
import org.charles.zhifou.model.dto.statistic.AppAnswerCountDTO;
import org.charles.zhifou.model.dto.statistic.AppAnswerResultCountDTO;
import org.charles.zhifou.service.UserAnswerService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <h1>应用统计分析</h1>
 *
 * @author charles
 * @version 1.0
 */
@RestController
@RequestMapping("/app/statistic")
@Slf4j
@Tag(name = "应用统计分析接口")
public class AppStatisticsController {

    @Resource
    private UserAnswerService userAnswerService;

    /**
     * 按被回答次数统计前 10 个被回答的应用
     * @return
     */
    @GetMapping("/answer_count")
    @Operation(summary = "按被回答次数统计前 10 个被回答的应用")
    @AuthCheck(neededRole = UserRoleEnum.ADMIN)
    public BaseResponse<List<AppAnswerCountDTO>> getAppAnswerCount() {
        List<AppAnswerCountDTO> result = userAnswerService.doAppAnswerCount();
        return ResponseUtils.success(result);
    }

    /**
     * 按应用 id 统计各答案结果
     * @param appId
     * @return
     */
    @GetMapping("/answer_result_count")
    @Operation(summary = "按应用 id 统计各答案结果")
    @AuthCheck(neededRole = UserRoleEnum.ADMIN)
    public BaseResponse<List<AppAnswerResultCountDTO>> getAppAnswerResultCount(
            @NotNull(message = "appId 不能为空")
            @Min(value = 1, message = "appId 必须大于 0")
            Long appId
    ) {
        return ResponseUtils.success(userAnswerService.doAppAnswerResultCount(appId));
    }

}
